package com.eric.nioclient;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.eric.nioclient.service.Logic;
import com.eric.nioclient.utils.SystemUtils;

public class LoginActivity extends BaseActivity implements Logic.ServiceListener{

    EditText vHost,vName,vRoom;
    Button vJoin;
    Logic logic = Logic.me();
    String user ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if(App.me().serviceAvailable()){
            gotoMainActivity();
            return;
        }

        setContentView(R.layout.activity_login);
        initView();

    }

    private void initView(){
        vHost = (EditText)findViewById(R.id.chat_host);
        vName = (EditText)findViewById(R.id.chat_name);
        vRoom = (EditText)findViewById(R.id.chat_room);
        vJoin = (Button) findViewById(R.id.join);
        vJoin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(App.me(), SystemUtils.getLocalHost(),Toast.LENGTH_LONG).show();
                if(!check()){
                    Toast.makeText(App.me(), "参数不全，检查参数", Toast.LENGTH_SHORT).show();
                    return;
                }
                App.me().startService(vHost.getText().toString(),8888);
                showLoading();
//                gotoMainActivity();
            }
        });
    }

    private boolean check(){
        String content = vHost.getText().toString();
        if(TextUtils.isEmpty(content))return false;
        content = vName.getText().toString();
        if(TextUtils.isEmpty(content))return false;
        content = vRoom.getText().toString();
        if(TextUtils.isEmpty(content))return false;

        return true;
    }

    private void gotoMainActivity(){
        Intent intent = new Intent(this,MainActivity.class);
        startActivity(intent);
        finish();
    }

    @Override
    public void started() {
        Log.w(TAG,"service started .");
    }

    @Override
    public void stoped(Object obj) {
        Log.w(TAG,"service stop .");
    }

    @Override
    public void connectActive() {
        Log.w(TAG,"connectActive .");
        user = vName.getText().toString() ;
        logic.login(user,vRoom.getText().toString());
    }

    @Override
    public void receivedMsg(String sendUser, String msg) {

    }

    @Override
    public void receivedNotice(String msg) {

    }

    @Override
    public void loginSuccess() {
        hideLoading();
        gotoMainActivity();
    }

    @Override
    public void loginFail(String msg) {
        hideLoading();
        Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
    }

    @Override
    protected void onResume() {
        super.onResume();
        logic.addListener(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        logic.removeListener(this);
    }
}
